package com.qn.example.controller;

import com.qn.example.pojo.Page;
import com.qn.example.pojo.R;
import com.qn.example.pojo.Setmeal;
import com.qn.example.pojo.dto.DishDto;
import com.qn.example.pojo.dto.SetmealDto;
import com.qn.example.service.impl.SetmealServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 21276
 * @version 1.0
 * @date 2023/5/16 10:16
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Resource
    private SetmealServiceImpl setmealService;
    @PostMapping
    public R saveSetmeal(@RequestBody SetmealDto setmealDto, HttpSession session){
        //获取登录用户的ID
        Long empId = (Long) session.getAttribute("user");
        //补数据
        setmealDto.setCreateUser(empId);
        setmealDto.setUpdateUser(empId);
        //调用业务层的方法
        setmealService.saveSetmeal(setmealDto);
        return R.success("添加套餐成功");
    }


    /**
     * 分页
     */
    @GetMapping("/page")
    public R page(@RequestParam(value = "page",defaultValue = "1") Integer page,
                  @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,String name){
        //调用业务层的获取每一页对象的方法

        Page pageBean = setmealService.findByPage(page,pageSize,name);
        return  R.success(pageBean);
    }

    //批量删除
    @DeleteMapping
    public R delete(@RequestParam("ids") List<Long> ids){
        System.out.println("删除的id："+ids);
        setmealService.delete(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    public R dishStatusByStatus(@PathVariable("status") Integer status,@RequestParam("ids") List<Long> ids){
        System.err.print("修改菜品状态：" + status);

        System.err.print("修改菜品id：");
        ids.forEach(System.err::println);


        //2. 交给service
        setmealService.dishStatusByStatus(status,ids);
        return R.success("修改菜品状态成功");
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        System.err.println(id);
        Setmeal setmeal = setmealService.findById(id);
        return R.success(setmeal);
    }

    @GetMapping("/list")
    public R list(Long categoryId,Integer status){
        List<SetmealDto> setmealDtoList =  setmealService.list(categoryId,status);
        return R.success(setmealDtoList);
    }

}
